import Mock from "mockjs"
import storage from "../src/utils/storage";
const users = [
    {
        admin: "admin",
        token: "admin"
    },
    {
        user: "000000",
        token: "user"
    },
]

const setRes = (code = 200, token = "", msg = "成功") => {
    return {
        code,
        data: token,
        msg
    }
}

const publicRoutes = [
    {
        path: "/dashboard",
        name: "dashboard",
        meta: {
            title: "仪表盘",
            icon: "Stopwatch",
            index: 2
        }
    },
    {
        path: "/setting",
        name: "setting",
        meta: {
            title: "系统设置",
            icon: "setting",
            index: 4
        }
    },
    {
        path: "https://gitee.com/sslc_7474/vue3-js-cms",
        name: "sslc_cms",
        isChain: true,
        meta: {
            title: "项目地址",
            icon: "Link",
            index: 6
        }
    },
    {
        path: "meun-1",
        name: "meun-1",
        meta: {
            title: "多级菜单",
            icon: "list",
            index: 5
        },
        children: [
            {
                path: "/menu-1-1",
                name: "menu-1-1",
                meta: {
                    title: "1-1",
                    index: 1
                }
            },
            {
                path: "/menu-1-2",
                name: "menu-1-2",
                meta: {
                    title: "1-2",
                    index: 2
                },
                children: [
                    {
                        path: "/menu-1-2-1",
                        name: "menu-1-2-1",
                        meta: {
                            title: "1-2-1",
                            index: 1,
                        }
                    },
                    {
                        path: "/menu-1-2-2",
                        name: "menu-1-2-2",
                        meta: {
                            title: "1-2-2",
                            index: 1,
                        }
                    }
                ]
            },
        ]
    }
]
const privateRoutes = [
    {
        path: "/menu",
        name: "menu",
        meta: {
            title: "菜单管理",
            icon: "List",
            index: 3
        },
        children: [
            {
                path: "/addMenuList",
                name: "addMenuList",
                meta: {
                    title: "添加菜单",
                    index: 1
                }
            },
            {
                path: "/menuList",
                name: "menuList",
                meta: {
                    title: "菜单列表",
                    index: 2
                }
            }
        ]
    },
    {
        path: "/staff",
        name: "staff",
        meta: {
            title: "成员管理",
            icon: "User",
            index: 1
        },
        children: [{
            path: "/staffList",
            name: "staffList",
            meta: {
                title: "成员列表",
                index: 1
            }
        }, {
            path: "/leaveManage",
            name: "leaveManage",
            meta: {
                title: "请假管理",
                index: 3
            }
        },
        {
            path: "/auth",
            name: "auth",
            meta: {
                title: "权限管理",
                index: 2
            }
        }]
    },
]
const api = [
    {
        url: "/user/login",
        type: "post",
        response: (option) => {
            const { userName, password } = JSON.parse(option.body)
            let res = {};
            let status = 0;
            users.map((item) => {
                if (item[userName] === password) {
                    res = setRes(200, item.token, "登录成功");
                    status = 1;
                } else {
                    if (status === 0) {
                        res = setRes(500, "", "账号或密码错误");
                    }
                }
            })
            return res;
        }
    },
    {
        url: "/user/userInfo",
        type: "get",
        response: (option) => {
            const token = storage.getItem("token");
            const data = Mock.mock(
                {
                    userInfo: {
                        auth: 1,
                        userName: '@name',
                        phone: "15965845258",
                    },
                    privateRoutes: token === "admin" ? [...privateRoutes, ...publicRoutes] : [...publicRoutes]
                },
            )
            return {
                code: 200,
                data,
                msg: "成功"
            }
        }
    }
]


export default api